package com.pdstudio.carrecom.ui.activity.owner;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pdstudio.carrecom.R;
import com.pdstudio.carrecom.api.HttpExecuteJson;
import com.pdstudio.carrecom.api.ServiceHelper;
import com.pdstudio.carrecom.api.HttpExecuteJson.httpReturnJson;
import com.pdstudio.carrecom.app.AppContext;
import com.pdstudio.carrecom.bean.ResultInfo;
import com.pdstudio.carrecom.logger.Logger;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 
 * 类: ActivityRescue <br>
 * 描述: 我是车主——维修救援 <br>
 * 作者: ws <br>
 * 时间: 2015-6-15 下午11:32:03
 */
public class ActivityRescue extends Activity implements OnClickListener {

	private static final String TAG = "ActivityRescue";

	private static final int MSG_WHAT_ABOUT = 1;

	private TextView txtTitle;
	private ImageView ivBack;

	private ResultInfo mResultInfo;

	private AppContext mAppContext = AppContext.getInstance();

	private TextView mRescueNumber;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_owner_rescue);
		initTitle();
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();

		getRescueInfo();
	}

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			Logger.i(TAG, msg);
			switch (msg.what) {
			case MSG_WHAT_ABOUT:// 关于
				if (mResultInfo != null) {
					mRescueNumber.setText(mResultInfo.data.toString());
				}
				break;

			default:
				break;
			}
		}
	};

	private LinearLayout mLinearLayoutMessageCenter;

	private LinearLayout mLinearLayoutBxxb;

	private LinearLayout mLinearLayoutRescue;

	/**
	 * 方法: initTitle <br>
	 * 描述: 初始化页面标题 <br>
	 * 作者: ws <br>
	 * 时间: 2015-5-24 下午5:46:11
	 */
	private void initTitle() {
		txtTitle = (TextView) findViewById(R.id.title_text_nav);

		txtTitle.setText("维修救援");

		ivBack = (ImageView) findViewById(R.id.title_back);
		ivBack.setOnClickListener(this);
	}

	private void initView() {
		mRescueNumber = (TextView) findViewById(R.id.owner_rescue_phone);

		mLinearLayoutMessageCenter = (LinearLayout) findViewById(R.id.LinearLayout02);
		mLinearLayoutBxxb = (LinearLayout) findViewById(R.id.LinearLayout01);
		mLinearLayoutRescue = (LinearLayout) findViewById(R.id.owner_bxxb);

		mLinearLayoutMessageCenter.setOnClickListener(this);
		mLinearLayoutBxxb.setOnClickListener(this);
		mLinearLayoutRescue.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			break;
		case R.id.LinearLayout02:
			// 用intent启动拨打电话
			Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
					+ mResultInfo.data.toString()));
			startActivity(intent);
			break;
		case R.id.LinearLayout01:
			//用intent启动拨打电话  
            Intent intent1 = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+"122"));  
            startActivity(intent1);  
			break;
		case R.id.owner_bxxb:
			//用intent启动拨打电话  
            Intent intent2 = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+"95518"));  
            startActivity(intent2);  
			break;
		}
	}

	/**
	 * 方法: getRescueInfo <br>
	 * 描述: 获取救援电话 <br>
	 * 作者: ws <br>
	 * 时间: 2015-5-24 下午8:40:10
	 */
	private void getRescueInfo() {
		try {
			HttpExecuteJson http = new HttpExecuteJson(this,
					mGetAbountInfoListener);
			String url = ServiceHelper.OwnerRescuePhone;
			Map<String, Object> rps = new HashMap<String, Object>();
			rps.put("uid", mAppContext.getUserId());

			http.get(url, rps);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private httpReturnJson mGetAbountInfoListener = new httpReturnJson() {

		@Override
		public void onSuccess(String result) {
			Logger.json(result);
			ResultInfo resultInfo = null;
			try {
				resultInfo = new Gson().fromJson(result,
						new TypeToken<ResultInfo>() {
						}.getType());
				if ((resultInfo != null)
						&& (resultInfo.msg
								.equals(ServiceHelper.msg_code_sucess))) {
					try {
						mResultInfo = resultInfo;
						Message msg = new Message();
						msg.obj = mResultInfo;
						msg.what = MSG_WHAT_ABOUT;
						mHandler.sendMessage(msg);

					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {

				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		@Override
		public void onFailure(int error, String msg) {

		}

		@Override
		public void onCancel() {

		}
	};

}
